<?php
/**
 * Created by PhpStorm.
 * User: DMF
 * Date: 2017/4/7
 * Time: 15:55
 */
namespace Admin\Controller;

#header("Content-Type: text/html; charset=utf-8")

class ArticleController extends \Common\Controller\BaseController
{
	private $_model,$_type;
    public function __construct(){
        parent::__construct();
        $this->_model=new \Admin\Model\CommentsModel();
		$this->_type = M('tab')->select();
    }
    //写文章
    public function write(){

        if(!empty($_POST)){
            $db = M('list');
            $data['title'] = I('title');
            
			//将文章关键字字符串X,Y,Z变为数组的array(0=>'X',1=>'Y',2=>'Z')
			$separator = ',';
			$string = I('type');
			$array=explode($separator,$string);
				
			//查找对应的字符串的值
			
			foreach($array as $k => $v){
				foreach($this->_type as $kk => $vv){
					if($v==$vv['name']){
						$data['type'] = $data['type'].','.$vv['id'];
					}
				}
			}
			
			$data['type2'] = I('type2');
            $data['context'] = I('context');
            $data['time'] = I('time');
            $data['visited'] = 0;
            $db->add($data);

            $this->redirect("Index/index");
        }else{
            //标签生成
            $db = M('tab');
            $tags = $db->select();
            $this->assign('tags',$tags);
            $this->display();
        }

    }
    //全部文章
    public function allcontent($num=0){
		
        $user = M('list');
        $cnt = $user->count('id');
        $multiple = 8;
        $page = new \Think\Page($cnt,$multiple);
        //var_dump($page);
        $show = $page->show();
        $list = $user->order('time')->limit(($num*$multiple).','.$page->listRows)->select();
		$count = 0;
		foreach($list as $k1 => $v1){
			//将文章关键字字符串X,Y,Z变为数组的array(0=>'X',1=>'Y',2=>'Z')
			$separator = ',';
			$string = $v1['type'];
			$array=explode($separator,$string);
					
			//查找对应的字符串的值
			foreach($array as $k => $v){
				foreach($this->_type as $kk => $vv){
					if($v==$vv['id']){
						$tags2[$count][]=$vv['name'];
					}
				}
			}
			$count++;
		}
		#echo '<pre>';
		#var_dump($tags2);
		#echo '</pre>';
		#die;
		$pageCnt = ceil($cnt*1.0/$multiple);
		$this->assign('tags2',$tags2);
		
        $this->assign('list',$list);
        //var_dump($count.$show);
        $this->assign('nowNum',$num);
		$this->assign('nextPage',$num>=$pageCnt-1?$pageCnt-1:$num+1);
		$this->assign('prePage',$num==0?0:$num-1);
		/*
		echo $count;
		echo '<br/>';
		echo $count*1.0/$multiple;
		echo '<br/>';
		echo ceil($count*1.0/$multiple);
		die;
		*/
        $this->assign('count',$pageCnt);
        $this->display();
    }
    //单篇文章
    public function content($id){
        $db = M('list');
        $data['id'] = $id;
        $text = $db->where($data)->select();
        //ip获取
        $ip = $this->getClientIp();
        //设置session成功返回true否则false
        if($this->setSession($ip."~$id",$ip."~$id")){
            $arr['visited'] = $text[0]['visited']+1;
            $db->where('id='.$id)->save($arr);
        }
        //var_dump($text);
        //var_dump(session());
		
		//将文章关键字字符串X,Y,Z变为数组的array(0=>'X',1=>'Y',2=>'Z')
		$separator = ',';
		$string = $text[0]['type'];
		$array=explode($separator,$string);
				
		//查找对应的字符串的值
		foreach($array as $k => $v){
			foreach($this->_type as $kk => $vv){
				if($v==$vv['id']){
					$tags2[]=$vv['name'];
				}
			}
		}
		
		$this->assign('tags2',$tags2);
        $this->assign('context',$text);
        //查找评论
        $db = M('comments');
        $comments = $db->where(array("belong"=>$id))->order('time asc')->select();
        //var_dump($comments);die;
        // var_dump($comments);die;
        $this->assign("comments",$comments);
        $this->display();
    }
	 //增加文章评论
    public function addcomment(){
        $data = array(
            'parent'=>I('parent')?I('parent'):"",
            'belong'=>I('id'),
            'content'=>I('content'),
            'time'=>I('time'),
            'email'=>"无",
            'username'=>session('username')?session('username'):"游客",
            'type'=>0,
        );
		if($data['content']==''){
				echo 'llbnwk';
				return;
		}
        //新增留言数据
        if(!$this->_model->setMessageData($data)){
            echo "留言添加失败！";
            //$this->error("留言添加失败！");
        }else{
            D('list')->where('id='.$data['belong'])->setInc('commentnum',1);
        }
    }
    //首页
    public function index(){
        $this->display();
    }
    //文章列表
    public function lst($num=0){
        //文章的查找
        $user = M('list');
        $count = $user->count();
        $multiple = 8;
        $page = new \Think\Page($count,$multiple);
        //var_dump($page);
        $show = $page->show();
        $list = $user->order('time desc')->limit(($num*$multiple).','.$page->listRows)->select();
		$pageCnt= ceil($count/$multiple);
        $this->assign('list',$list);
        //var_dump($count.$show);
        $this->assign('nowNum',$num);
		$this->assign('nextPage',$num>=$pageCnt-1?$pageCnt-1:$num+1);
		$this->assign('prePage',$num==0?0:$num-1);
        $this->assign('count',$pageCnt);
        $this->display();
    }

    //文章标签管理
    public function category(){

    }
	
	//文章数据修改
	public function articleUpd(){
        //修改页面显示页面
		if(empty($_POST)){
			#echo "成功进入更新页面1！";
			#die;
			$id = I('id');
			$user = M('list');
			$data = $user->where('id='.$id)->find();
			if($data){
				
				//将文章关键字字符串X,Y,Z变为数组的array(0=>'X',1=>'Y',2=>'Z')
				$separator = ',';
				$string = $data['type'];
				$array=explode($separator,$string);
				
				//查找对应的字符串的值
				foreach($array as $k => $v){
					foreach($this->_type as $kk => $vv){
						if($v==$vv['id']){
							$tags2[]=$vv['name'];
						}
					}
				}
				foreach($this->_type as $k => $v){
					if(!in_array($v['name'],$tags2)){
						$tags1[] = $v['name'];
					}
				}
				#echo '<pre>';
				#var_dump($tags1);
				#var_dump($tags2);
				#echo '</pre>';
				#die;
				$this->assign('tags1',$tags1);
				$this->assign('tags2',$tags2);
				$this->assign('id',$id);
				$this->assign('data',$data);
				$this->display('articleUpd');
			}else{
				$this->error('修改方面出错了');
			}
		}else{//修改页面提交操作

			#echo "成功进入更新页面2！";
			#die;
			$db = M('list');
            $data['title'] = I('title');
            
			//将文章关键字字符串X,Y,Z变为数组的array(0=>'X',1=>'Y',2=>'Z')
			$separator = ',';
			$string = I('type');
			$array=explode($separator,$string);
				
			//查找对应的字符串的值
			$data['type']='';
			foreach($array as $k => $v){
				foreach($this->_type as $kk => $vv){
					if($v==$vv['name']){
						if($data['type']==''){
							$data['type'] = $vv['id'];
						}else{
							$data['type'] = $data['type'].','.$vv['id'];
						}
						
					}
				}
			}
			
			$data['type2'] = I('type2');
            $data['context'] = I('context');
            $data['lasttime'] = I('time');
			$data['id'] = I('id');
			$flag = $db->save($data);
			#echo '<pre>';
			#echo $string;
			#var_dump($data);
			#echo '</pre>';
			#die;
			
			if($flag!==false){
				$this->success('文章更新成功！',U('lst'));
			}else{
				$this->success('文章更新失败！');
			}
		}
	}
	
    //删除文章数据
    public function del(){
        $id = I('id');
        $user = M('list');
        $flag = $user->where("id = $id")->delete();

        if(!$flag){
            $this->error("删除文章失败",U('Index/index'));
        } else{
            $this->success("删除文章成功",U('Index/index'));
        }
    }

    //文章标签
    public function tags(){
        $db = M('tab');
        $tags = $db->select();
        $this->assign("tags",$tags);
        $this->display();
    }
    //文章标签添加
    public function tagadd(){

        if(!empty($_POST)){
            $data['name'] = I('name');
            $db = M('tab');
            $result = $db->add($data);
            if($result){
                $this->success('添加标签成功',U('tags'));
            }else{
                $this->error('添加标签失败',U('tags'));
            }
        }else {
            $this->display();
        }
    }
    //文章标签修改
    public function tagupd(){
        $data = array(
            'id' => I('id'),
            'name' => I('name'),
        );
        if(I('type')==1) {
            $this->assign('tag', $data);
            $this->display();
        }else{
            $db = M('tab');
            $result = $db->save($data);
            if($result){
                $this->success('修改标签成功',U('tags'));
            }else{
                $this->error('修改标签失败',U('tags'));
            }
        }
    }
    //文章标签删除
    public function tagdel(){
        $db = M('tab');
        $id = I('id');
       $result = $db->where("id=".$id)->delete();
        if($result){
            $this->success('删除标签成功',U('tags'));
        }else{
            $this->error('删除标签失败',U('tags'));
        }
    }
}